<?php echo $this->Html->css(array('hProducts', 'date', 'datepicker', 'normalize')) ?>
<?php echo $this->Html->script('date'); ?>

<div class="sales index">
  <section class="search dateForm">

    <h2><?php echo __('Resumen de Ventas'); ?></h2>

    <?php echo $this->Form->create('Sales', array('action' => 'index')); ?>
    <label for="from">Fecha Desde</label>
    <input type="text" id="from" name="from" value="<?php echo isset($from) ? $from : '' ?>"/>
    <label for="to">Hasta</label>
    <input type="text" id="to" name="to" value="<?php echo isset($to) ? $to : '' ?>" />
    <?php echo $this->Form->end(array('label' => '', 'class' => 'buttonLupa2', 'div' => false, 'id' => 'searchDate')) ?>
    <?php echo $this->Html->Link('Exportar', array('action' => 'export', isset($from) ? $from : '', isset($to) ? $to : ''), array('id' => 'export')); ?>

  </section>
  <hr>

  <table cellpadding="0" cellspacing="0">
    <tr>
      <th><?php echo $this->Paginator->sort('Vendedor'); ?></th>
      <th><?php echo $this->Paginator->sort('Punto de Venta'); ?></th>
      <th><?php echo $this->Paginator->sort('Fecha de Venta'); ?></th>
      <th><?php echo $this->Paginator->sort('Numero de Ticket'); ?></th>
      <th><?php echo $this->Paginator->sort('Total ($)'); ?></th>

      <?php if ($this->Session->read('Auth.User.role') === 'admin') : ?>
        <th class="actions"><?php echo __('Opciones'); ?></th>
      <?php endif; ?>

    </tr>

    <?php $totalSuma = 0; ?>
    <?php foreach ($sales as $sale): ?>
      <tr>


        <td>
          <?php echo $this->Html->link($sale['User']['name'], array('controller' => 'users', 'action' => 'view', $sale['User']['id'])); ?>
        </td>
        <td>
          <?php echo $this->Html->link($sale['Shop']['name'], array('controller' => 'shops', 'action' => 'view', $sale['Shop']['id'])); ?>
        </td>
        <td><?php echo $this->Time->format('d/m/Y H:i:s', $sale['Sale']['date']) ?>&nbsp;</td>

        <td><?php echo h($sale['Sale']['ticket_number']); ?>&nbsp;</td>
        <td><?php
//                Acumulo el total sumando cada linea de la venta
        $totalLine = 0;
        foreach ($sale['LineSale'] as $line) {
          $totalLine += $line['total_line'];
        }

        $totalSuma += $totalLine;

        echo $totalLine;
          ?>&nbsp;</td>
        <?php if ($this->Session->read('Auth.User.role') === 'admin') : ?>
          <td class="actions">
            <?php echo $this->Html->link(__('Detalle'), array('controller' => 'LineSales', 'action' => 'view', $sale['Sale']['id'])); ?>
            <?php // echo $this->Html->link(__('Editar'), array('action' => 'edit', $sale['Sale']['id'])); ?>
            <?php echo $this->Form->postLink(__('Borrar'), array('action' => 'delete', $sale['Sale']['id']), null, __('¿Realmente desea eliminar la venta?', $sale['Sale']['id'])); ?>
          </td>
        <?php endif; ?>

      </tr>
    <?php endforeach; ?>
    <tfoot>
      <tr >
        <th></th>
        <th></th>
        <th></th>
        <th>Total :</th>
        <th class="total">  <?php echo $totalSuma ?></th>
        <th></th>

      </tr>
    </tfoot>
  </table>

  <?php echo $this->element('footer/paginator') ?>

</div>




<!--Show General Menu-->
<?php echo $this->element('menues/general') ?>
